<?php
class ActionModel extends CI_Model{
	
	public function __construct(){
		parent::__construct();
	}
	
	public function is_table_free($table_id){
		$this->db->select('status');
		$this->db->from('tables');
		$this->db->where('_id', $table_id);
		$this->db->where('status', 1);
		
		//run select
		$query = $this -> db -> get();
		
		if ($query->num_rows() > 0){
			return true;
		}
		
		return false;
	}
	
	public function create_new_bill($table_id, $user_id){
		$current_time = time();
		$data['bill_code'] = $table_id.$user_id.$current_time;
		$data['user_id'] = $user_id;
		$data['date'] = date('y-m-d', $current_time);
		$data['start_time'] = date('y-m-d h-i-s',$current_time);
		$data['table_id'] = $table_id;
		
		$result = $this->db->insert('bills', $data);
		if ($result){
			return $this->db->insert_id();
		}else{
			return false;
		}
	}
	
	public function update_table_status($table_id, $new_status, $extra_table){
		$this->db->where('_id', $table_id);
		$data['status'] = $new_status;
		$data['extra_table'] = $extra_table;
		$this->db->update('tables', $data);
		
		$affect = $this->db->affected_rows();
		
		return $affect;
	}
	
	public function bill_detail($bill_id){
		$this->db->select('*');
		$this->db->from('bills');
		$this->db->where('_id', $bill_id);
		
		//run select
		$query = $this -> db -> get();
		
		if ($query->num_rows() > 0){
			$result = $query->result();
			return $result[0];
		}
		
		return false;
	}
	
	public function dish_detail($dish_id){
		$this->db->select('_id, cost');
		$this->db->from('dishes');
		$this->db->where('_id', $dish_id);
		
		//run select
		$query = $this -> db -> get();
 
        if ($query->num_rows() > 0) {
            $result = $query->result();
            return $result[0];
        }
        return false;
	}
	
	public function is_exist_bill($bill_id){
		$this->db->select('*');
		$this->db->from('bills');
		$this->db->where('_id', $bill_id);
		
		//run select
		$query = $this -> db -> get();
		if ($query->num_rows() > 0){
			$result = $query->result();
			$end_time = $result[0]->end_time;
			if ($end_time == null){
				return true;
			}
			
			return false;
		}
		
		return false;
	}
	
	public function add_dish_bill($info){
		$this->db->insert('bill_description', $info);
	}
	
	public function bill_description_detail($bill_id){
		$this->db->select('*');
		$this->db->from('bill_description');
		$this->db->where('bill_id', $bill_id);
		
		//run select
		$query = $this -> db -> get();
		
		if ($query->num_rows() > 0){
			$result = $query->result();
			return $result;
		}
		
		return false;
	}
	
	public function serve_dish($id){
		$current_time = time();
		$data['serve_time'] = date('y-m-d h-i-s', $current_time);
		$this->db->where('_id', $id);
		$this->db->update('bill_description', $data);
		
		$affect = $this->db->affected_rows();
		
		return $affect;
	}
	
	public function update_bill_description($bill_id){
		$this->db->where('bill_id', $bill_id);
		$this->db->where('serve_time', null);
		$this->db->delete('bill_description');
	}
	
	public function update_bill($bill_id, $info){
		$this->db->where('_id', $bill_id);
		$this->db->update('bills', $info);
		
		$affect = $this->db->affected_rows();
		
		return $affect;
	}
	
	public function update_extra_table_status($table_id){
		$data['status'] = 1;
		$data['extra_table'] = null;
		$this->db->where('extra_table', $table_id);
		$this->db->where('status', 3);
		$this->db->update('tables', $data);
		
		$affect = $this->db->affected_rows();
		
		return $affect;
	}
	
	public function feedback_dish($id, $feedback){
		$data['feedback'] = $feedback;
		$this->db->where('_id', $id);
		$this->db->update('bill_description', $data);
		
		$affect = $this->db->affected_rows();
		
		return $affect;
	}
	
	public function isCanCancelBill($table_id){
		$this->db->from('bills b');
		$this->db->join('bill_description as bd', 'b._id = bd.bill_id');
		$this->db->where('b.table_id', $table_id);
		$this->db->where('end_time IS NULL');
		$this->db->where('serve_time IS NOT NULL');
		
		$result = $this->db->count_all_results();
	}
	
	public function deleteBill($table_id){
		$this->db->where('table_id', $table_id);
		$this->db->delete('bills');
	}
	
	public function cancelTableStatus($table_id){
		$data['status'] = 1;
		$this->db->where('_id', $table_id);
		$this->db->or_where('extra_table', $table_id);
		$this->db->update('tables', $data);
	}
	
	public function bookingTable($table_id, $object){
		$this->db->where('_id', $table_id);
		$this->db->update('tables', $object);
		
		$affect = $this->db->affected_rows();
		return $affect;
	}
	
	public function bill_detail_by_user($user_id){
		$this->db->select('*');
		$this->db->from('bills');
		$this->db->where('user_id', $user_id);
		$this->db->where('end_time IS NULL');
		
		//run select
		$query = $this -> db -> get();
		
		if ($query->num_rows() > 0){
			return $query->result();
		}
		
		return false;
	}
	
	/*
	 * detail of table
	 */
	public function table_detail($table_id){
		$this->db->select('t._id as _id, t.table_name as name, number, t.manager_id as manager_id, u.first_name as first_name, u.last_name as last_name, t.status as status_id, ts.status as status, t.description as description, t.image as image, booking_status, booking_time, booking_description');
		$this->db->from('tables as t');
		$this->db->join('users as u', 'u._id = t.manager_id');
		$this->db->join('table_status as ts', 'ts._id = t.status');
		$this->db->where('t._id', $table_id);
		
		//run select
		$query = $this -> db -> get();
 
        if ($query->num_rows() > 0) {
            $result = $query->result();
            return $result[0];
        }
        return false;
	}
	
	public function delete_dish_bill($bill_id, $food_id){
		$this->db->where('bill_id', $bill_id);
		$this->db->where('food_id', $food_id);
		$this->db->where('serve_time IS NULL');
		
		$this->db->delete('bill_description');
		$affect = $this->db->affected_rows();
		return $affect;
	}
	
	public function modify_dish_bill($bill_id, $food_id, $info){
		$this->db->where('bill_id', $bill_id);
		$this->db->where('food_id', $food_id);
		$this->db->where('serve_time IS NULL');
		
		$this->db->update('bill_description', $info);
		$affect = $this->db->affected_rows();
		return $affect;
	}
	
	public function serve_dishes($bill_id, $food_id, $info){
		$this->db->where('bill_id', $bill_id);
		$this->db->where('food_id', $food_id);
		$this->db->where('serve_time IS NULL');
		
		$this->db->update('bill_description', $info);
		$affect = $this->db->affected_rows();
		return $affect;
	}
	
	public function cancel_booking_table($table_id){
		$this->db->where('_id', $table_id);
		$info['booking_status'] = null;
		$info['booking_time'] = null;
		$info['booking_description'] = null;
		
		$this->db->update('tables', $info);
		
		$affect = $this->db->affected_rows();
		
		return $affect;
	}
}